//
//  TimeBarView.cpp
//  FruitCrush
//
//  Created by gaoyu on 15/7/19.
//
//

#include "ShowGoldView.h"
#include "GoldSaveModel.h"
#include "MarketMasonryView.h"
#include "MarketGoldView.h"

USING_NS_CC;

#define TEXT_FONTSIZE   20
#define TEXT_FONTNAME   "arial"
#define GOLD_TEXT_COLOR      (Color3B::WHITE)
#define DIAMOND_BG_X      155
#define GOLD_ICON_LEFT    2
#define GOLD_ICON_BOTTOM  2
#define GOLD_NUM_LEFT     33
#define GOLD_NUM_BOTTOM   4
#define GOLD_MENU_X       125
#define GOLD_MENU_Y       0
#define DIAMOND_ICON_LEFT    (DIAMOND_BG_X+GOLD_ICON_LEFT)
#define DIAMOND_ICON_BOTTOM  GOLD_ICON_BOTTOM
#define DIAMOND_NUM_LEFT     (DIAMOND_BG_X+GOLD_NUM_LEFT)
#define DIAMOND_NUM_BOTTOM   GOLD_NUM_BOTTOM
#define DIAMOND_MENU_X       (DIAMOND_BG_X+GOLD_MENU_X)
#define DIAMOND_MENU_Y       GOLD_MENU_Y



typedef enum {
    ZBg = 0,
    ZIcon,
    ZText,
    ZMenu,
    

} ZIndex;

typedef enum {
    TagMenu,
    TagGold,
    TagDiamond,
    
    
} Tag;

ShowGoldView::ShowGoldView()
:goldNumLabel(NULL)
{
}

ShowGoldView* ShowGoldView::create()
{
	ShowGoldView *pRet = new(std::nothrow) ShowGoldView();
    if (pRet && pRet->init()){
        pRet->autorelease();
        return pRet;
    }
    else
    {
        delete pRet;
        pRet = NULL;
        return NULL;
    }
}

void ShowGoldView::onEnter()
{
    int goldNum = GoldSaveModel::getInstance()->getGoldNum();
    __String *numStr = __String::createWithFormat("%d",goldNum);
	goldNumLabel->setString(numStr->getCString());
    
    int diamondNum = GoldSaveModel::getInstance()->getDiamondNum();
    __String *numStrD = __String::createWithFormat("%d",diamondNum);
    diamondNumLabel->setString(numStrD->getCString());
    Layer::onEnter();
}

bool ShowGoldView::init()
{
    if (!Layer::init()) {
        return false;
    }
    bgInit();
    goldInit();
    menuInit();
    return true;
}

void ShowGoldView::bgInit()
{
	auto cache = SpriteFrameCache::getInstance();
	cache->addSpriteFramesWithFile("CommonIcon.plist");

    auto bg = Sprite::createWithSpriteFrameName("MapGoldViewBg.png");
    bg->setAnchorPoint(Vec2::ZERO);
    bg->setPosition(Vec2::ZERO);
    this->addChild(bg,ZBg);
    
    auto bgDiamond = Sprite::createWithSpriteFrameName("MapGoldViewBg.png");
    bgDiamond->setAnchorPoint(Vec2::ZERO);
    bgDiamond->setPosition(DIAMOND_BG_X,0);
    this->addChild(bgDiamond,ZBg);
}

void ShowGoldView::goldInit()
{
    auto goldIcon = Sprite::createWithSpriteFrameName("GoldIcon60.png");
    goldIcon->setAnchorPoint(Vec2::ZERO);
    goldIcon->setPosition(Point(GOLD_ICON_LEFT,GOLD_ICON_BOTTOM));
    this->addChild(goldIcon,ZIcon);

    int goldNum = GoldSaveModel::getInstance()->getGoldNum();
    __String *numStr = __String::create("");
    numStr->initWithFormat("%d",goldNum);
    goldNumLabel = LabelTTF::create(numStr->getCString(), TEXT_FONTNAME, TEXT_FONTSIZE);
    goldNumLabel->setAnchorPoint(Vec2::ZERO);
    goldNumLabel->setPosition(Point(GOLD_NUM_LEFT,GOLD_NUM_BOTTOM));
    goldNumLabel->setColor(GOLD_TEXT_COLOR);
    addChild(goldNumLabel,ZText);
    
    auto diamondIcon = Sprite::createWithSpriteFrameName("DiamondIcon60.png");
    diamondIcon->setAnchorPoint(Vec2::ZERO);
    diamondIcon->setPosition(Point(DIAMOND_ICON_LEFT,DIAMOND_ICON_BOTTOM));
    this->addChild(diamondIcon,ZIcon);
    
    int diamondNum = GoldSaveModel::getInstance()->getDiamondNum();
    __String *numStrD = __String::create("");
    numStrD->initWithFormat("%d",diamondNum);
    diamondNumLabel = LabelTTF::create(numStrD->getCString(), TEXT_FONTNAME, TEXT_FONTSIZE);
    diamondNumLabel->setAnchorPoint(Vec2::ZERO);
    diamondNumLabel->setPosition(Point(DIAMOND_NUM_LEFT,DIAMOND_NUM_BOTTOM));
    diamondNumLabel->setColor(GOLD_TEXT_COLOR);
    addChild(diamondNumLabel,ZText);

}

void ShowGoldView::menuInit()
{
    Menu *addMenu = Menu::create();
    addMenu->setAnchorPoint(Vec2::ZERO);
    addMenu->setPosition(Vec2::ZERO);
    addMenu->setTag(TagMenu);
    
    MenuItemImage *goldMenu = MenuItemImage::create("AddButton60.png","AddButtonSelected60.png","AddButton60.png",CC_CALLBACK_1(ShowGoldView::menuCallback, this));
    goldMenu->setAnchorPoint(Vec2::ZERO);
    goldMenu->setPosition(GOLD_MENU_X,GOLD_MENU_Y);
    goldMenu->setTag(TagGold);
    addMenu->addChild(goldMenu);
    
    MenuItemImage *diamondMenu = MenuItemImage::create("AddButton60.png","AddButtonSelected60.png","AddButton60.png",CC_CALLBACK_1(ShowGoldView::menuCallback, this));
    diamondMenu->setAnchorPoint(Vec2::ZERO);
    diamondMenu->setPosition(DIAMOND_MENU_X,DIAMOND_MENU_Y);
    diamondMenu->setTag(TagDiamond);
    addMenu->addChild(diamondMenu);

    this->addChild(addMenu,ZMenu);
}

void ShowGoldView::menuCallback(Ref *sender)
{
    MenuItemImage *item = (MenuItemImage*)sender;
    if(item->getTag() == TagGold){
    	auto scene = MarketGoldView::createScene();
        Director::getInstance()->pushScene(scene);
    }else if(item->getTag() == TagDiamond){
    	auto scene = MarketMasonryView::createScene();
        Director::getInstance()->pushScene(scene);
    }
}

void ShowGoldView::unVisableButton()
{
	Menu *addMenu = (Menu *)getChildByTag(TagMenu);
	addMenu->setVisible(false);
}

void ShowGoldView::showUpdate()
{
    int goldNum = GoldSaveModel::getInstance()->getGoldNum();
    __String *numStr = __String::createWithFormat("%d",goldNum);
	goldNumLabel->setString(numStr->getCString());

    int diamondNum = GoldSaveModel::getInstance()->getDiamondNum();
    __String *numStrD = __String::createWithFormat("%d",diamondNum);
    diamondNumLabel->setString(numStrD->getCString());
    Layer::onEnter();
}
